package com.karst.webnotes.chapter04
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.BitmapMaterialTools;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
	
	internal class FlipMaterialExample2 extends Sprite
	{
		public function FlipMaterialExample2()
		{
			super();
			createView() ;
		}
		
		private var view : BasicView = null ;
		private var bmpMaterial : BitmapFileMaterial = null ;
		private var materials : MaterialsList = null ;
		private var cube : Cube = null ;
		private function createView() : void
		{
			if( view == null )
			{
				view = new BasicView( 0, 0, true, true ) ;
				view.buttonMode = true ;
				view.addEventListener( Event.ENTER_FRAME, rateHandler ) ;
			}
			
			if( bmpMaterial == null )
			{
				bmpMaterial = new BitmapFileMaterial( "01.jpg" ) ;
				bmpMaterial.addEventListener( FileLoadEvent.LOAD_COMPLETE, loadedHandler ) ;
			}
			
			
			if( materials == null )
			{
				materials = new MaterialsList() ;
				materials.addMaterial( bmpMaterial, "front" ) ;
				materials.addMaterial( bmpMaterial, "back" ) ;
				materials.addMaterial( bmpMaterial, "left" ) ;
				materials.addMaterial( bmpMaterial, "right" ) ;
				materials.addMaterial( bmpMaterial, "top" ) ;
				materials.addMaterial( bmpMaterial, "bottom" ) ;
			}
			
			if( cube == null )
			{
				cube = new Cube( materials, 400, 400, 450, 2 ) ;
				cube.rotationY = 45 ;
			}
			
			addChild( view ) ;
			view.scene.addChild( cube ) ;
		}
		
		private function rateHandler( event : Event ) : void
		{
			if( view )
				view.singleRender() ;
			if( cube )
			{
				cube.localRotationX += 2 ;
				cube.localRotationY-- ;
				cube.localRotationZ-- ;
			}
		}
		
		private function loadedHandler( event : FileLoadEvent ) : void
		{
			bmpMaterial.opposite = true ;
			bmpMaterial.precise = true ;
			bmpMaterial.smooth = true ;
			BitmapMaterialTools.mirrorBitmapX( bmpMaterial.bitmap ) ;
		}
	}
}